/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package eu.fastl.dragster.log.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import eu.fastl.dragster.log.service.LogAnalyserService;
import eu.fastl.dragster.log.service.LogParserResult;
import eu.fastl.dragster.model.ExecutionRecord;


/**
 * Simple implementation of {@link LogAnalyserService}
 * @author Robert Benak
 *
 */
public class LogAnalyserServiceSimpleImpl implements LogAnalyserService {

	public Map<String, Long> getMaxExecutionTimeByType(LogParserResult parserResult) {
		Set<String> types = parserResult.getExecutionRecordsByType().keySet();
		Map<String,Long> maxExecutionByTypeMap = new HashMap<String,Long>();
		long max = 0;
		for (String type : types) {
			max = 0;
			List<ExecutionRecord> executionRecords = parserResult.getExecutionRecordsByType().get(type);
			for (ExecutionRecord executionRecord : executionRecords) {
				if (executionRecord.getExecutionTime() > max) {
					max = executionRecord.getExecutionTime();
				}
			}
			maxExecutionByTypeMap.put(type, max);
		}
		return maxExecutionByTypeMap;
	}

	public Map<String, Long> getMinExecutionTimeByType(LogParserResult parserResult) {
		Set<String> types = parserResult.getExecutionRecordsByType().keySet();
		Map<String,Long> maxExecutionByTypeMap = new HashMap<String,Long>();
		long min = 0;
		for (String type : types) {
			min = 0;
			List<ExecutionRecord> executionRecords = parserResult.getExecutionRecordsByType().get(type);
			for (ExecutionRecord executionRecord : executionRecords) {
				if (executionRecord.getExecutionTime() < min || min == 0) {
					min = executionRecord.getExecutionTime();
				}
			}
			maxExecutionByTypeMap.put(type, min);
		}
		return maxExecutionByTypeMap;
    }

	public Map<String, Long> getAvgExecutionTimeByType(LogParserResult parserResult) {
		Set<String> types = parserResult.getExecutionRecordsByType().keySet();
		Map<String,Long> maxExecutionByTypeMap = new HashMap<String,Long>();
		long overallTime = 0;
		int count = 0;
		for (String type : types) {
			overallTime = 0;
			count = 0;
			List<ExecutionRecord> executionRecords = parserResult.getExecutionRecordsByType().get(type);
			for (ExecutionRecord executionRecord : executionRecords) {
				overallTime+=executionRecord.getExecutionTime();
				count++;
			}
			maxExecutionByTypeMap.put(type, overallTime / count);
		}
		return maxExecutionByTypeMap;
	}

}
